package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.ChargeHistory;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class UserChargeHistoryAdapter extends BaseAdapter {

    private ChargeHistory chargeHistory;
    private CarInfo account;
    private UserBean user;

    private Context context;

    public UserChargeHistoryAdapter(Context context, ChargeHistory chargeHistory, UserBean user, CarInfo account) {
        this.context = context;
        this.chargeHistory = chargeHistory;
        this.user = user;
        this.account = account;
        Collections.sort(this.chargeHistory.getROWS_DETAIL(), (o1, o2) -> o2.getChargetime().compareTo(o1.getChargetime()));
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public Object getItem(int position) {
        return chargeHistory.getROWS_DETAIL().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_user_charge_history, parent, false);
            holder = new ViewHolder();
            holder.time = convertView.findViewById(R.id.time);
            holder.title = convertView.findViewById(R.id.title);
            holder.value = convertView.findViewById(R.id.value);
            holder.money = convertView.findViewById(R.id.money);
            holder.left = convertView.findViewById(R.id.left);
            holder.charge_time = convertView.findViewById(R.id.charge_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (chargeHistory.getROWS_DETAIL() != null && chargeHistory.getROWS_DETAIL().size() > 0
                && user.getROWS_DETAIL() != null && user.getROWS_DETAIL().size() > 0
                && account.getROWS_DETAIL() != null && account.getROWS_DETAIL().size() > 0) {

            ChargeHistory.ROWSDETAILBean chargeHistoryBean = chargeHistory.getROWS_DETAIL().get(position);
            UserBean.ROWS_DETAIL userBean = user.getROWS_DETAIL().get(position);
            CarInfo.ROWSDETAILBean carInfoBean = account.getROWS_DETAIL().get(position);

            String time = chargeHistoryBean.getChargetime();
            if (time == null) {
                holder.time.setText("充值时间为空");
                holder.charge_time.setText("充值时间为空");
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
                try {
                    Date date = simpleDateFormat.parse(time);
                    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd EEEE", Locale.CHINA);

                    holder.time.setText(simpleDateFormat2.format(date).replace(" ", "\n"));
                    holder.charge_time.setText("充值时间\n" + time.substring(0, time.length() - 5));
                } catch (Exception ignored) {
                }
            }

            holder.title.setText(String.format("充值人：%s", userBean.getPname()));

            holder.value.setText(String.format("车牌号：%s", carInfoBean.getCarnumber()));

            holder.money.setText(String.format("充值：%d", chargeHistoryBean.getMoney()));

            HttpUtils.post("get_car_account_balance",
                    new HttpUtils().setParam("CarId", position + 1)
                            .setParam("UserName", MainActivity.userName),
                    response -> {
                        Map responseMap = new Gson().fromJson(response.toString(), Map.class);
                        holder.left.setText(String.format("余额：%d", (int) Utils.parseDouble(responseMap.get("Balance").toString())));
                    });
        } else {
            holder.money.setText("暂无充值历史记录");
        }

        return convertView;
    }

    static class ViewHolder {
        TextView time, title, value, money, left, charge_time;
    }
}
